<?php
/**
 * page.php 
 *
 * @author     Xiaodc
 * @copyright  Copyright (c) 2005-2015 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

function pager($page, $total, $filter)
{
    $l_page = '<a href="%s">上一页</a>';
    $r_page = '<a href="%s">下一页</a>';
    $c_page = '<button>第%d页/共%d页</button>';
    $page = intval($page);
    $page = $page>0 ? $page : 1;
    $output = '';
    
    if($page == $total && $total == 1)
    {
        $output = sprintf($c_page, $page, $total);
    }elseif($page>=$total)
    {
        $filter['page'] = $total-1;
        $url = create_url($filter);
        $output .= sprintf($l_page, $url);
        $output .= sprintf($c_page, $total, $total);
    }elseif ($page<$total && $page == 1)
    {
        $filter['page'] = $page+1;
        $url = create_url($filter);
        $output .= sprintf($c_page, $page, $total);
        $output .= sprintf($r_page, $url);
    }else{
        
        $filter['page'] = $page-1;
        $url = create_url($filter);
        $output .= sprintf($l_page, $url);
        $output .= sprintf($c_page, $page, $total);
        $filter['page'] = $page+1;
        $url = create_url($filter);
        $output .= sprintf($r_page, $url);
    }
    
    return $output;
}

function create_url($filter)
{
    $params = http_build_query($filter);
    $url = 'http://localhost/test.php?'.$params;
    
    return $url;
}
 
 